#基类Base
import time

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains    #鼠标事件
from selenium.webdriver.common.keys import Keys                     #键盘事件
from selenium.webdriver.support.select import Select                #下拉选择框
from datetime import datetime
class Base():
    #定义初始化方法
    def __init__(self,driver):
        self.driver = driver
    #浏览器方法
    def goto(self,url):
        self.driver.get(url)
    #定位元素方法
    def find_element(self,loc):
        try:
            WebDriverWait(self.driver,20,0.5).until(
                lambda driver: self.driver.find_element(*loc).is_displayed
            )
            return self.driver.find_element(*loc)
        except Exception as e:
            #时间戳
            t = time.strftime('%Y%m%d_%H_%M_%S')
            #截屏
            self.driver.save_screenshot(t+'yichang.png')
            print(e)
    #截屏
    # def screanshot(self,x):
        # self.driver.save_screenshot('x')
    #点击事件方法
    def click(self,loc):
        self.find_element(loc).click()
    #输入事件方法
    def send_keys(self,loc,info):
        self.find_element(loc).send_keys(info)
    #文本方法
    def text(self,loc):
        return print(self.find_element(loc).text)

    #frame框架
    def frame(self,loc):
        self.driver.switch_to.frame(self.find_element(loc))

    #退出框架
    def iframe(self):
        self.driver.switch_to.default_content()
    #滚动事件——任意位置x
    def scroll_to_x(self,x):
        self.driver.execute_script(f'scrollTo(0,{x})')
    #滚动事件——滚动到底部
    def scrollTo(self):
        self.driver.execute_script('scrollTo(0,document.body.scrollHeight)')

    #切换页面
    def new_window(self):
        self.driver.switch_to.window(self.driver.window_handles[-1])

    #鼠标事件
    #(双击事件)
    def double_click(self,loc):
        ActionChains(self.driver).double_click(self.find_element(loc)).perform()

    # (三击事件)
    def Three_click(self,loc):
        ActionChains(self.driver).double_click(self.find_element(loc)).click(self.find_element(loc)).perform()

    #(悬停事件)
    def move_to_element(self,loc):
        ActionChains(self.driver).move_to_element(self.find_element(loc)).perform()

    #（长按事件）
    def click_and_hold(self,loc):
        ActionChains(self.driver).click_and_hold(self.find_element(loc)).perform()

    #(右击事件)
    def context_click(self,loc):
        ActionChains(self.driver).context_click(self.find_element(loc)).perform()

    #键盘事件
    #(回车事件)
    def Enter(self,loc):
        self.find_element(loc).send_keys(Keys.ENTER)
    #(全选事件)
    def Ctrl_a(self,loc):
        self.find_element(loc).send_keys(Keys.CONTROL,'a')
    #(复制事件)
    def Ctrl_c(self,loc):
        self.find_element(loc).send_keys(Keys.CONTROL,'c')
    #(粘贴事件)
    def Ctrl_v(self,loc):
        self.find_element(loc).send_keys(Keys.CONTROL,'v')
    #(剪切事件)
    def Ctrl_x(self,loc):
        self.find_element(loc).send_keys(Keys.CONTROL,'x')
    #(Tab事件)
    def Tab(self,loc):
        self.find_element(loc).send_keys(Keys.TAB)
    #(空格事件)
    def Space(self,loc):
        self.find_element(loc).send_keys(Keys.SPACE)
    #(退格事件)
    def Back_Space(self,loc):
        self.find_element(loc).send_keys(Keys.BACK_SPACE)

    #浏览器事件
    #（刷新）
    def refresh(self):
        self.driver.refresh()

    #（退出）
    def quite(self):
        self.driver.quite()

    #(窗口最大化)
    def max_window(self):
        self.driver.maximize_window()


    #下拉框选择
    # def Select_value(self,loc,info):
    #     targe = Select(self.find_element(loc))
    #     targe.select_by_value(info)
    # ##下拉框选择
    # def Select_index(self,loc,x):
    #     targe = Select(self.find_element(loc))
    #     targe.select_by_index(x)
    # ##下拉框选择
    # def Select_visible_text(self,loc,info):
    #     targe = Select(self.find_element(loc))
    #     targe.select_by_visible_text(info)

    #下拉菜单value值
    def Select_value(self,loc,info):
        targe = Select(self.find_element(loc))
        targe.select_by_value(info)
    #下拉菜单index值
    def Select_index(self,loc,x):
        targe = Select(self.find_element(loc))
        targe.select_by_index(x)
    #下拉菜单visible_text值
    def Select_visible_text(self,loc,info):
        targe = Select(self.find_element(loc))
        targe.select_by_visible_text(info)
    #弹框处理——确认
    def alert_accept(self,loc):
        alert = self.driver.switch_to.alert     #先确定好框架
        alert.accept()

    #弹框处理——取消
    def alert_dismiss(self):
        alert = self.driver.switch_to.alert     #确定好弹框
        alert.dismiss()




